/**
 * 
 */
package com.antilia.infomaresme.pages.details;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.UserItemAlert;
import com.antilia.infomaresme.link.JQIcon;
import com.antilia.infomaresme.link.JqAjaxLink;
import com.antilia.infomaresme.provider.UserItemAlertsLdm;

/**
 * @author Ernesto
 *
 */
public abstract class DetailsViewLinkPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private IModel<UserItemAlert> alertModel;
		
	/**
	 * @param id
	 * @param model
	 */
	public DetailsViewLinkPanel(String id, final IModel<Item> model) {
		super(id, model);

		UserItemAlert alert = InfomaresmeApplication.getQAApplication()
		.getAlertsDao().getAlert(InfomaresmeSession.getSession().getUser(), model.getObject());
		
		alertModel = new UserItemAlertsLdm(alert);
		
		JqAjaxLink link = new JqAjaxLink("details", JQIcon.ui_icon_zoomin, new ResourceModel("itemDetails")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				DetailsViewLinkPanel.this.onSee(target, model);				
			}
		};
		add(link);
		
		JQIcon icon;
		if(alert == null)
			icon = JQIcon.ui_icon_alert;
		else 
			icon = JQIcon.ui_icon_star;
			
		JqAjaxLink addallert = new JqAjaxLink("addalert", icon, new ResourceModel(alert==null?"actions.createNewAlerta":"actions.editAlerta")) {	
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				DetailsViewLinkPanel.this.onAlert(target, model, alertModel);				
			}
		};
		add(addallert);
		
		if(model.getObject().isAllowMessages()) {
			JqAjaxLink message = new JqAjaxLink("message", JQIcon.ui_icon_mail_closed, new ResourceModel("message.Send")) {	
				
				private static final long serialVersionUID = 1L;
	
				@Override
				public void onClick(AjaxRequestTarget target) {
					DetailsViewLinkPanel.this.onSendMessage(target, model);				
				}
			};
			add(message);
		} else {
			add(new org.apache.wicket.markup.html.basic.Label("message",  "&nbsp;").setEscapeModelStrings(false));
		}
	
		
	}

	public abstract void onSee(AjaxRequestTarget target, IModel<Item> model);
	
	public abstract void onAlert(AjaxRequestTarget target, IModel<Item> model,IModel<UserItemAlert> alertModel);
	
	public abstract void onSendMessage(AjaxRequestTarget target, IModel<Item> model);
		
}
